Option Explicit

' 将 dataFrame 数据转换为一个 markdown 表格字符串数据
'
' @param data IDataFrame 类型数据
' @optional-param firstIsSplitRow 数据的第一行是否是分割行
'                   默认值 = `False`，表示第一行不是分割行，是输入数据
'                   如果该参数为 `True`，则无论 data 的第一行是什么都会直接忽略
' @optional-param lineBreak 换行符
'                   默认值 = `vbCrLf`
' @return markdown 表格字符串
Public Function transDataFrameToMdTableTxt( _
    ByRef data As IDataFrame, _
    Optional ByRef firstIsSplitRow As Boolean = False, _
    Optional ByRef lineBreak As String = vbCrLf _
) As String
    ' 1. 获取列数和行数
    Dim colMaxIndex As Integer
    colMaxIndex = data.ColumnCount - 1
    Dim dataMaxIndex As Integer
    dataMaxIndex = data.Size - 1

    ' 2. 获取表头
    transDataFrameToMdTableTxt = joinMdTableTitleStr(data.ColumnNames)

    ' 3. 生成分割行
    Dim i As Long
    i = 0

    ' 如果第一行是分割行
    If firstIsSplitRow Then

        If dataMaxIndex <= 0 Then
            ' 如果当前没有数据，则按照表头的列数生成分割行
            transDataFrameToMdTableTxt = _
                    transDataFrameToMdTableTxt & _
                    lineBreak & _
                    joinMdTableSplitRowStr(colMaxIndex)
        Else
            transDataFrameToMdTableTxt = _
                    transDataFrameToMdTableTxt & _
                    lineBreak & _
                    joinMdTableRowStr(data, i, colMaxIndex)
            i = i + 1
        End If
    Else
        ' 3.3 如果第二行不是分割行，则按照表头的列数生成分割行
        transDataFrameToMdTableTxt = _
                transDataFrameToMdTableTxt & _
                lineBreak & _
                joinMdTableSplitRowStr(colMaxIndex)
    End If

    ' 4. 遍历生成数据
    For i = i To dataMaxIndex
        transDataFrameToMdTableTxt = _
                transDataFrameToMdTableTxt & _
                lineBreak & _
                joinMdTableRowStr(data, i, colMaxIndex)
    Next
End Function

' 生成表头字符串
Private Function joinMdTableTitleStr(ByRef cNames As Collection) As String
    Dim i As Integer
    Dim s As String

    joinMdTableTitleStr = joinMdTableTitleStr & "|"
    For i = 1 To cNames.Count
        s = cNames.Item(i)
        joinMdTableTitleStr = joinMdTableTitleStr & _
            " " & _
            UtilsString.escapeTargetStr(s, "|") & _
            " |"
    Next
End Function

' 根据一行数据生成 markdown table 的一行字符串字符串
Private Function joinMdTableRowStr( _
    ByRef data As IDataFrame, _
    ByRef rowIndex As Long, _
    ByRef colMaxIndex As Integer _
) As String
    Dim i As Integer
    Dim s As String

    joinMdTableRowStr = joinMdTableRowStr & "|"
    For i = 0 To colMaxIndex
        s = data.getDataByColIndex(rowIndex, i)
        joinMdTableRowStr = joinMdTableRowStr & _
            " " & _
            UtilsString.escapeTargetStr(s, "|") & _
            " |"
    Next
End Function

' 创建 markdown table 的分割行字符串
Private Function joinMdTableSplitRowStr(ByRef colMaxIndex As Integer) As String
    Dim j As Integer

    joinMdTableSplitRowStr = joinMdTableSplitRowStr & "|"
    For j = 0 To colMaxIndex
        joinMdTableSplitRowStr = joinMdTableSplitRowStr & _
            " - |"
    Next
End Function